package demo.authentication.client;

import com.google.api.gwt.oauth2.client.Auth;
import com.google.api.gwt.oauth2.client.AuthRequest;
import com.google.api.gwt.oauth2.client.Callback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class OAuthdemo implements EntryPoint {

	 private static final Auth AUTH = Auth.get();
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

//		try {
//			CalendarConnectionServiceAsync calConnect = GWT
//					.create(CalendarConnectionService.class);
//
//			calConnect.getConnection(new AsyncCallback<CalendarData>() {
//				public void onFailure(Throwable error) {
//				}
//
//				public void onSuccess(CalendarData result) {
//					// data=result;
//					listEntries(result);
//				}
//			});
//		} catch (Exception e) {
//			RootPanel.get("oauth").add(new Label(e.getLocalizedMessage()));
//		}

		addGoogleAuth();
	}

	

	  private static final String GOOGLE_AUTH_URL = "https://accounts.google.com/o/oauth2/auth";

	  // This app's personal client ID assigned by the Google APIs Console
	  // (http://code.google.com/apis/console).
	  private static final String GOOGLE_CLIENT_ID = "842209351820.apps.googleusercontent.com";
	  
	  
	  //private static final String secret="_ezsiUbgN5RpG-AXBK2sXCvl";

	  // The auth scope being requested. This scope will allow the application to
	  // read Buzz activities, comments, etc., as if it was the user.
	  private static final String BUZZ_READONLY_SCOPE = "http://www.google.com/calendar/feeds/default/allcalendars/full";

	  // Adds a button to the page that asks for authentication from Google.
	  private void addGoogleAuth() {
	    // Since the auth flow requires opening a popup window, it must be started
	    // as a direct result of a user action, such as clicking a button or link.
	    // Otherwise, a browser's popup blocker may block the popup.
	    Button button = new Button("Authenticate with Google");
	    button.addClickHandler(new ClickHandler() {
	      @Override
	      public void onClick(ClickEvent event) {
	        AuthRequest req = new AuthRequest(GOOGLE_AUTH_URL, GOOGLE_CLIENT_ID)
	            .withScopes(BUZZ_READONLY_SCOPE);

	        // Calling login() will display a popup to the user the first time it is
	        // called. Once the user has granted access to the application,
	        // subsequent calls to login() will not display the popup, and will
	        // immediately result in the callback being given the token to use.
	        AUTH.login(req, new Callback<String, Throwable>() {
	          @Override
	          public void onSuccess(String token) {
	          //  Window.alert("Got an OAuth token:\n" + token);
	            listEntries(token);
	          }

	          @Override
	          public void onFailure(Throwable caught) {
	            Window.alert("Error:\n" + caught.getMessage());
	          }
	        });
	      }
	    });
	    RootPanel.get().add(button);
	  }
	
	private void listEntries(String token) {

		try {
			CalendarConnectionServiceAsync calConnect = GWT
					.create(CalendarConnectionService.class);

			calConnect.getConnection(token,new AsyncCallback<CalendarData>() {
						public void onFailure(Throwable error) {
						}
	
						public void onSuccess(CalendarData result) {
//							data=result;
							RootPanel.get("oauth").add(new Label(result.getName()));
//							listEntries(result);
						}
					});
		} catch (Exception e) {
			RootPanel.get("oauth").add(new Label(e.getLocalizedMessage()));
		}

	}
}
